///*
// * To change this template, choose Tools | Templates
// * and open the template in the editor.
// */
//package cz.muni.fi.rum.reflector.telnet.core;
//
//import cz.muni.fi.rum.reflector.telnet.domainmodel.RapMessage;
//import cz.muni.fi.rum.reflector.telnet.domainmodel.RapRequest;
//import cz.muni.fi.rum.reflector.telnet.domainmodel.RapResponse;
//import cz.muni.fi.rum.sender.SenderManager;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.io.InputStreamReader;
//import java.io.OutputStreamWriter;
//import java.util.Collection;
//import java.util.Collections;
//import java.util.concurrent.BlockingQueue;
//import java.util.concurrent.LinkedBlockingQueue;
//
///**
// *
// * @author pmikulasek
// */
//@Deprecated
//public class Sender implements Runnable {
//
//    private BlockingQueue<RapRequest> senderFifo;
//    private BlockingQueue<RapMessage> receiverFifo;
//    private final OutputStreamWriter outputNetworkStream;
//    private boolean bufferMode = true;
//    private boolean cleanOutFifo = false;
//    private final BufferedReader inputNetworkStream;
//    private boolean canceled = false;
//    private static final Logger log = LoggerFactory.getLogger(Sender.class);
//@Deprecated
//    public Sender(BlockingQueue<RapMessage> receiverFifo, OutputStreamWriter outputNetworkStream, InputStreamReader inputNetworkStream) {
//        this.senderFifo = new LinkedBlockingQueue<RapRequest>();
//        this.receiverFifo = receiverFifo;
//        this.outputNetworkStream = outputNetworkStream;
//        this.inputNetworkStream = new BufferedReader(inputNetworkStream);
//    }
//@Deprecated
//    public void sendRapRequest(RapRequest request) {
//        try {
//            senderFifo.put(request);
//
//        } catch (InterruptedException ex) {
//            log.error("Unexpected interruption of sender queue", ex);
//            //rekurzivni volani!! v pripade neuspechu vlozeni do fronty zabezpeci znovuvlozeni.
//            sendRapRequest(request);
//        }
//    }
//@Deprecated
//    public synchronized void close() {
//        this.canceled = true;
//    }
//@Deprecated
//    public synchronized void cleanOutSenderFifo() {
//        cleanOutFifo = true;
//    }
//@Deprecated
//    public synchronized void setBufferMode() {
//        cleanOutFifo = false;
//        bufferMode = true;
//    }
//@Deprecated
//    public synchronized void setForceMode() {
//        bufferMode = false;
//    }
//@Deprecated
//    public Collection<RapRequest> getAllUnsentRapRequest() {
//        if (senderFifo.isEmpty()) {
//            return Collections.emptyList();
//        } else {
//            return Collections.unmodifiableCollection(senderFifo);
//        }
//    }
//@Deprecated
//    public void cancelUnSentRapRequest(RapRequest request) {
//        senderFifo.remove(request);
//    }
//@Deprecated
//    private void sentOneRequest() {
//        try {
//            RapRequest re = senderFifo.peek();
//            outputNetworkStream.write(re.getMessage());
//            outputNetworkStream.write(Core.CRLF);
//            outputNetworkStream.flush();
//            StringBuffer responseBuffer = new StringBuffer();
//            //pozor tento radek navic ovlivnuje chod Response parseru!!
//            responseBuffer.append(Core.CRLF);
//
//            while (true) {
//                if (inputNetworkStream.ready()) {
//                    String line;
//                    line = inputNetworkStream.readLine();
//                    if (!line.isEmpty()) {
//                        responseBuffer.append(line);
//                        responseBuffer.append(Core.CRLF);
//                    } else {
//                        responseBuffer.append(line);
//                        responseBuffer.append(Core.CRLF);
//                        if (inputNetworkStream.ready()) {
//                            line = inputNetworkStream.readLine();
//                            if (line.isEmpty()) {
//                                responseBuffer.append(Core.RESPONSE_END);
//                                break;
//                            }
//                        } else {
//                            responseBuffer.append(Core.RESPONSE_END);
//                            break;
//                        }
//                    }
//                } else {
//                    Thread.sleep(100);
//                }
//            }
//            receiverFifo.put(senderFifo.take());
//            RapResponse rapResponse = new RapResponse(responseBuffer.toString());
//            receiverFifo.put(rapResponse);
//            //TODO isSuccesResponse nic se nedeje, ale v momente kdy bude false, tak
//            //spustit telnet manager load session from reflector
////            try {
////                ResponseParser.checkError(rapResponse);
////            } catch (ResponseException ex) {
////                Logger.getLogger(Sender.class.getName()).log(Level.SEVERE, null, ex);
////            } catch (IllegalArgumentException ex) {
////                Logger.getLogger(Sender.class.getName()).log(Level.SEVERE, null, ex);
////            }
//
//        } catch (InterruptedException ex) {
//            //jestlize je preruseno vkladani do fronty
//            log.error("Unexpected interruption of sender queue", ex);
//        } catch (IOException ex) {
//            // v momente kdy selze prvni stream tak se to cely posle znovu
//            //ve druhem kroku se to posle do dalsi fronty k dalsimu zpracovani. to uz probiha venku
//            //toto vlakno funguje jako producent
//            log.error("IO problem while sending", ex);
//        }
//    }
//@Deprecated
//    @Override
//    public void run() {
//        while (!(canceled && senderFifo.isEmpty())) {
//            if (senderFifo.isEmpty()) {
//                try {
//                    cleanOutFifo = false;
//                    //uspani v pripade prazdne fronty
//                    Thread.sleep(1000);
//
//                } catch (InterruptedException ex) {
//                    //probuzeni vlakna ze spani nemelo by nastat
//                    log.error("Unexpected interruption while waiting for not empty queue.", ex);
//                    ex.printStackTrace();
//                }
//            } else {
//                if (bufferMode && !cleanOutFifo) {
//                    //nedelej nic cekej plni se ti fronta
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException ex) {
//                        //probuzeni vlakna ze spani nemelo by nastat
//                        log.error("Unexpected interruption while waiting for not empty queue.", ex);
//                    }
//                } else if (bufferMode && cleanOutFifo) {
//                    //vyprazdni frontu
//                    synchronized (this) {
//                        sentOneRequest();
//                        //v momente kdy predchozi odeslani vyprazdni posilaci frontu,
//                        //tak se opet nastavi na false a ceka na dalsi bufferove posilani
//                        if (senderFifo.isEmpty()) {
//                            cleanOutFifo = false;
//                        }
//                    }
//                } else {
//                    //v momente kdy neni buffer mode aktivni, tak se jenom posila
//                    sentOneRequest();
//                }
//            }
//        }
//    }
//}
